package lanou3g.com.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import lanou3g.com.Activity.R;
import lanou3g.com.DataBean.DataBean;
import lanou3g.com.DataBean.SubCardsBean;
import lanou3g.com.Util.CommonUtil;
import lanou3g.com.smart_image_view.SmartImageView;


public class Delicious_food_adapter extends BaseAdapter{

    public static final String LOG_TAG = "RecommendAdapter";

    private LayoutInflater inflater;

    private List<DataBean> dataBeans;

    private List<SubCardsBean> subCardsBeans;

    public Delicious_food_adapter(Context c,List<DataBean> beans) {

        inflater = LayoutInflater.from(c);

        dataBeans = beans;
        Log.i("log", "beans.size() = " + beans.size());

    }

    @Override
    public int getCount() {
        if (CommonUtil.isListNotNull(dataBeans)){
            return dataBeans.size();
        }
        return 0;
    }


    @Override
    public Object getItem(int position) {
        if (CommonUtil.isListNotNull(dataBeans)){
            return dataBeans.size();
        }
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
subCardsBeans = new ArrayList<SubCardsBean>();
        Holder holder = null;
        if (null == convertView){
            convertView = inflater.inflate(R.layout.adapter_delicious_food,null);
            holder = new Holder();
            holder .dataListItemFoodImageView =
                    (SmartImageView) convertView.findViewById(R.id.dataListItemFoodImageView);
            holder.dataListItemUserName =
                    (TextView) convertView.findViewById(R.id.dataListItemUserName);
            holder.dataListItemUserSign =
                    (TextView) convertView.findViewById(R.id.dataListItemUserSign);
            holder.dataListItemUserIcImageView =
                    (SmartImageView) convertView.findViewById(R.id.dataListItemUserIcImageView);
            holder.dataListItemUserExplain =
                    (TextView) convertView.findViewById(R.id.dataListItemContent);
            convertView.setTag(holder);

        }else {
            holder = (Holder) convertView.getTag();
        }
        if (CommonUtil.isListNotNull(dataBeans)) {
            DataBean dataBean = dataBeans.get(position);
            if (null != dataBean.getSubCards() && DataBean.TYPE_CONTAINER_CARD.equals(dataBean)) {

                subCardsBeans = dataBean.getSubCards();

            }
        }
        if (CommonUtil.isListNotNull(subCardsBeans)) {
            Log.i(LOG_TAG, "subCardsBeans is not null");
            SubCardsBean subCardsBean = null;
            SubCardsBean subCardsBeanContent = null;
            SubCardsBean subCardsBeanImage = null;
            for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
                if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBean = subCardsBeanTemp;
                }
                if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanImage = subCardsBeanTemp;
                }
                if ("SlideCard".equals(subCardsBeanTemp.getType())) {
                    subCardsBeanImage = subCardsBeanTemp;
                }
                if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanContent = subCardsBeanTemp;
                    break;
                }
            }
                if (subCardsBean.getUser() == null){

                    Log.i(LOG_TAG,"subCardsBean.getUser()==null");
                }

                holder.dataListItemFoodImageView.setImageUrl(subCardsBean.getUser().getAvatar(),
                        SmartImageView.TYPE_ROUND);
                holder.dataListItemUserName.setText(subCardsBean.getTitle());
                holder.dataListItemUserSign.setText(subCardsBean.getDesc());
                holder.dataListItemUserExplain.setText(subCardsBeanContent.getContent());

            String imageUrl = "";
            if (null == subCardsBeanImage.getPics()){
                imageUrl = subCardsBeanImage.getPic().getUrl();
            }else {
                imageUrl = subCardsBeanImage.getPics().get(0).getUrl();
            }
            holder.dataListItemFoodImageView.setImageUrl(imageUrl);
        }

        return convertView;
    }
    class Holder{
        SmartImageView dataListItemUserIcImageView;
        TextView dataListItemUserName;
        TextView dataListItemUserSign;
        TextView dataListItemUserExplain;
        SmartImageView dataListItemFoodImageView;

    }
}
